﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AssetsSys.BLL.AutoMapper;
using AssetsSys.DAL;
using AssetsSys.Dto;
using AssetsSys.Dto.Assets;
using AssetsSys.Dto.Type;
using AssetsSys.model;
using Type = AssetsSys.model.Type;

namespace AssetsSys.BLL
{
    /// <summary>
    /// 资产类型
    /// </summary>
    public class TypeManger
    {
        /// <summary>
        /// 获取类型列表
        /// </summary>
        /// <returns></returns>
        public static async Task<List<TypeModel>> GetList()
        {
            using (var typSvc = new TypeService())
            {
                var type = await typSvc.GetAll().OrderByDescending(x => x.CreateTime).ToListAsync();
                return type.MapTo<List<TypeModel>>();
            }
        }

        /// <summary>
        /// 添加类型
        /// </summary>
        /// <param name="code"></param>
        /// <param name="typeName"></param>
        /// <param name="ageLimit"></param>
        /// <returns></returns>
        public static async Task<bool> Create(int code, string typeName, int ageLimit)
        {
            using (var typSvc = new TypeService())
            {
                await typSvc.CreateAsync(new Type()
                {
                    AgeLimit = ageLimit,
                    Code = code,
                    TypeName = typeName
                });
                return true;
            }
        }

        /// <summary>
        /// 删除类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task<bool> Delete(Guid id)
        {
            using (var typSvc = new TypeService())
            {
                var model = await typSvc.GetOne(id);
                if (model == null)
                {
                    return false;
                }
                await typSvc.Remove(id);
                return true;
            }
        }

        /// <summary>
        /// 获取类型详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task<TypeModel> GetById(Guid id)
        {
            using (var typSvc = new TypeService())
            {
                return (await typSvc.GetOne(id)).MapTo<TypeModel>();
            }
        }

        /// <summary>
        /// 变更类型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static async Task<bool> Update(TypeModel model)
        {
            using (var typSvc = new TypeService())
            {
                var oldModel = await typSvc.GetOne(model.Id);
                if (oldModel == null)
                {
                    return false;
                }
                oldModel.Code = model.Code == 0 ? oldModel.Code : model.Code;
                oldModel.AgeLimit = model.AgeLimit == 0 ? oldModel.AgeLimit : model.AgeLimit;
                oldModel.TypeName = model.TypeName ?? oldModel.TypeName;
                await typSvc.EditAsvnc(oldModel);
            }
            return true;
        }


        public static async Task<List<CountModel>> GetCountInfo()
        {
            var countNum = new List<CountModel>();
            using (var typSvc = new TypeService())
            {
                //获取情况信息
                var types = await typSvc.GetAll().OrderByDescending(x => x.CreateTime).ToListAsync();
                using (var assSvc = new AssetsService())
                {
                    var assetsList = await assSvc.GetAll().Select(x => new AssetsResponseModel
                    {
                        Id = x.Id,
                        AssetsNum = x.AssetsNum,
                        AssetsName = x.AssetsName,
                        AssetsDescribe = x.AssetsDescribe,
                        Decimal = x.Decimal,
                        Type = x.Type.TypeName,
                        AgeLimit = x.Type.AgeLimit,
                        Source = x.Source.SourceName,
                        Status = x.Status.StatusName,
                        PrincipalUser = x.PrincipalUser.UserName,
                        PrincipalDepartment = x.PrincipalUser.Department.DepartmentName,
                        StorageUser = x.StorageUser.UserName,
                        StorageDepartment = x.StorageUser.Department.DepartmentName,
                        CreateTime = x.CreateTime
                    }).ToListAsync();
                    types.ForEach(x =>
                    {
                        var name = x.TypeName;
                        var count = assetsList.Count(y => y.Type == x.TypeName).ToString();
                        countNum.Add(new CountModel()
                        {
                            CountName = name,
                            CountNum = count,
                            Units = "件"
                        });
                    });
                    return countNum;
                }
            }
        }
    }
}
